package lk.mrt.cse.wihidum.client;

/**
 * Created by IntelliJ IDEA.
 * User: Will
 * Date: Feb 28, 2010
 * Time: 9:44:05 PM
 * To change this template use File | Settings | File Templates.
 */

/**
 *Thisclass can be used by a subscription client or a EventSink service to form a standardized unsubscription message
 * and send it to the relevant end point.
 */
import org.apache.axis2.client.*;
import org.apache.log4j.*;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.*;
import org.apache.axis2.context.*;
import org.apache.axis2.wsdl.WSDLConstants;
import lk.mrt.cse.wihidum.util.WSEventingConstants;
import lk.mrt.cse.wihidum.util.WihidumConstants;


public class UnsubscriptionClient
{
   private static final Logger lgger=Logger.getLogger(UnsubscriptionClient.class);
   private UnsubscriptionClientBean unsubscriptionClientBean;

   public UnsubscriptionClient(UnsubscriptionClientBean unsubscriptionClientBean)
   {
       this.unsubscriptionClientBean=unsubscriptionClientBean;
   }
   public String unsubscribe()
   {
       OMElement responseElement=null;
       SOAPEnvelope responseEnvelope=null;
       try
       {
           ServiceClient serviceClient=new ServiceClient();
           OperationClient operationClient=serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
           MessageContext outContext=new MessageContext();
           Options opts=outContext.getOptions();
           opts.setAction(WihidumConstants.Actions.Unsubscribe);
           //opts.setReplyTo(unsubscriptionClientBean.getEventSinkAddress());
           opts.setTo(unsubscriptionClientBean.getBrokerAddress());

           SOAPEnvelope unsubcriptionEnvelope=createUnsubscriptionEnelope();

           outContext.setEnvelope(unsubcriptionEnvelope);
           operationClient.addMessageContext(outContext);
           operationClient.execute(true);
           MessageContext inMsgtCtx = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);

           responseEnvelope = inMsgtCtx.getEnvelope();


       }
       catch(Exception ex)
       {
           ex.printStackTrace();           
       }
       if(responseEnvelope.getBody().getFirstElement()!=null)
       {
           return "Subscription Manager deleted subscription successfully";
       }
       else
           return "Error occured in deleting subscription";

   }
    private SOAPEnvelope createUnsubscriptionEnelope()
    {
        SOAPFactory factory=OMAbstractFactory.getSOAP12Factory();
        SOAPEnvelope unsubscriptionEnvelope=factory.getDefaultEnvelope();

        OMNamespace wseventingNS=factory.createOMNamespace(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.EVENTING_PREFIX);
        unsubscriptionEnvelope.declareNamespace(wseventingNS);
        OMNamespace wsaNS=factory.createOMNamespace(WSEventingConstants.ADDRESSING_NAMESPACE,WSEventingConstants.ADDRESSING_PREFIX);
        unsubscriptionEnvelope.declareNamespace(wsaNS);
        OMNamespace wihidumNS=factory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
        unsubscriptionEnvelope.declareNamespace(wihidumNS);
        SOAPHeader soapHeader=unsubscriptionEnvelope.getHeader();

        OMElement identifierElement=factory.createOMElement(WSEventingConstants.ElementNames.Identifier,wseventingNS);
        identifierElement.setText(unsubscriptionClientBean.getIdentifier());

        soapHeader.addChild(identifierElement);
        
        SOAPBody soapBody=unsubscriptionEnvelope.getBody();

        OMElement unsubscriptionElement =factory.createOMElement(WSEventingConstants.ElementNames.Unsubscribe, wseventingNS);
        soapBody.addChild(unsubscriptionElement);
        return unsubscriptionEnvelope;
    }

}
